package com.dmcloud.dm.auth.config;

import com.dmcloud.dm.auth.service.DmUserDetails;
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.token.TokenEnhancer;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * 对jwt进行增强
 * 存储一些开源见的信息
 * Created by xiaodao
 * date: 2019/11/5
 */
@Component("dMJwtTokenEnhancer")
public class DMJwtTokenEnhancer implements TokenEnhancer {
    @Override
    public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
        Map<String,Object> jwtInfo = new HashMap<>(16);
        DmUserDetails dmUser = (DmUserDetails) authentication.getUserAuthentication().getPrincipal();
        jwtInfo.put("userId",dmUser.getId());
        jwtInfo.put("deptId",dmUser.getDeptId());
        jwtInfo.put("tenantId",dmUser.getTenantId());
        jwtInfo.put("username",dmUser.getName());
        jwtInfo.put("sex",dmUser.getSex());
        jwtInfo.put("account",dmUser.getAccount());
        ((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(jwtInfo);
        return accessToken;
    }
}
